package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.constant.RedisConstant;
import net.zjitc.entity.Carouse;
import net.zjitc.service.ICarouseService;
import net.zjitc.until.MinioUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-02 11:59:58
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "轮播图")
public class CarouseController {
    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioUtils minioutils;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Reference
    private ICarouseService carouseService;

    ResponseResult result=new ResponseResult();
    @ApiOperation("上传轮播图")
    @PostMapping("carousel/upload")
    public ResponseResult upload(@RequestParam("imgFile") MultipartFile imgFile){

        System.out.println(imgFile);
        try {
            List<String> upload = minioutils.upload(new MultipartFile[]{imgFile});
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,upload.get(0));
            result.setSuccess("图片上传成功",upload.get(0));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("图片上传失败");
            return result;
        }
    }

    @ApiOperation("获取所有轮播图")
    @GetMapping("carousel")
    public ResponseResult getCarousel(@RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                      @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Carouse> carouse=carouseService.getcarouse(pagenum,pagesize);
        if (carouse.getTotal()>0){
            result.setSuccess("获取成功",carouse);
        }else {
            result.setError("获取失败");
        }
        return result;
    }

    @ApiOperation("根据id查找轮播图")
    @GetMapping("carousel/{id}")
    public ResponseResult getCarouselById(@PathVariable("id") Integer id){
        Carouse carouse = carouseService.getById(id);
        if (carouse!=null){
            result.setSuccess("获取轮播图成功",carouse);
        }else {
            result.setError("获取轮播图失败");
        }
        return result;
    }
    @ApiOperation("添加轮播图")
    @PostMapping("carousel")
    public ResponseResult AddCarousel(@RequestBody Carouse carouse){
        Date time=new Date();
        carouse.setAddtime(time);
        boolean save = carouseService.save(carouse);
        if (save){
            result.setCreateSuccess("添加轮播图成功");
        }else {
            result.setError("添加轮播图失败");
        }
        return result;
    }
    @ApiOperation("修改轮播图")
    @PutMapping("carousel/{id}")
    public ResponseResult UpdateCarousel(@RequestBody Carouse carouse,@PathVariable("id") int id){
        carouse.setId(id);
        boolean update = carouseService.updateById(carouse);
        if (update){
            result.setSuccess("修改轮播图成功");
        }else {
            result.setError("修改轮播图失败");
        }
        return result;
    }
    @ApiOperation("删除轮播图")
    @DeleteMapping("carousel/{id}")
    public ResponseResult deleteCarousel(@PathVariable("id") int id){

        int delete= carouseService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除轮播图成功");
        }else {
            result.setError("删除轮播图失败");
        }
        return result;
    }


}

